﻿#region Copyright information
// 
// Copyright © 2005-2013 Yongkee Cho. All rights reserved.
// 
// This code is a part of the Biological Object Library and governed under the terms of the
// GNU Lesser General  Public License (LGPL) version 2.1 which accompanies this distribution.
// For more information on the LGPL, please visit http://bol.codeplex.com/license.
// 
// - Filename: BinaryGenomeBase.cs
// - Author: Yongkee Cho
// - Email: yongkeecho@gmail.com
// - Date Created: 2012-12-08 8:26 AM
// - Last Modified: 2013-01-25 3:59 PM
// 
#endregion
using System;
using System.Linq;
using BOL.Linq;

namespace BOL.Algorithms.Optimization.EvolutionaryAlgorithms
{
    public abstract class BinaryArrayGenomeBase : ArrayGenomeBase<bool>
    {
        #region Constructor

        protected BinaryArrayGenomeBase(Random random, bool[] sequence)
            : base(random, sequence) { }

        protected BinaryArrayGenomeBase(Random random, int length)
            : this(random, new int[length].Select(x => random.Next() % 2 == 0).ToArray()) { }

        #endregion

        #region Public method

        public override void Mutate(double mutationRate)
        {
            var indicesToBeMutated = Enumerable.Range(0, Length).OrderByRandom(Random).Take((int)(Length * mutationRate));
            foreach (var index in indicesToBeMutated)
                Sequence[index] = !Sequence[index];
        }

        public override string ToString()
        {
            return String.Format("({0})", new string(Sequence.Select(x => (x) ? '1' : '0').ToArray()));
        }

        #endregion
    }
}
